//
//  MSWriteCommentViewController.m
//  MinShengBank
//
//  Created by chen wei on 13-10-17.
//  Copyright (c) 2013年 Hq. All rights reserved.
//

#import "MSWriteCommentViewController.h"
#import "RequestServer.h"
#import "RequestServerDelegate.h"

@interface MSWriteCommentViewController ()<RequestServerDelegate>
@property(nonatomic, retain)NSString*     contentID;
@property(nonatomic, retain)UITextView*  textView;
@end

@implementation MSWriteCommentViewController

- (id)initWithContentId:(NSString*)contentId
{
    if ([super init] != nil) {
        self.contentID = [[NSString alloc] initWithString:contentId];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel;
    if (isIOS7) {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 30)];
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 65, 300, 150)];
    } else {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 45, 300, 150)];
    }
    titleLabel.text = @"写评论";
    titleLabel.textColor = [UIColor colorWithHexString:@"4b9ead"];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.view addSubview:titleLabel];
    
    
    self.textView.textColor = [UIColor blackColor];//设置textview里面的字体颜色
    self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置字体名字和字体大小
    self.textView.delegate = self;//设置它的委托方法
    self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
    self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.textView.scrollEnabled = YES;//是否可以拖动
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.textView.layer.masksToBounds=YES;
    self.textView.layer.borderWidth=1.0;
    [self.textView becomeFirstResponder];
    self.textView.layer.borderColor=[[UIColor colorWithHexString:@"4b9ead"] CGColor];
    [self.view addSubview:self.textView];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (isIOS7) {
        [cancelButton setFrame:CGRectMake(320-12-28-42, 32, 24, 24)];
    } else [cancelButton setFrame:CGRectMake(320-12-28-42, 12, 24, 24)];
    [cancelButton setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor clearColor];
    [cancelButton addTarget:self action:@selector(cancelView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (isIOS7) {
        [confirmButton setFrame:CGRectMake(320-12-28, 33, 24, 24)];
    } else [confirmButton setFrame:CGRectMake(320-12-28, 12, 24, 24)];
    [confirmButton setImage:[UIImage imageNamed:@"submit.png"] forState:UIControlStateNormal];
    confirmButton.backgroundColor = [UIColor clearColor];
    [confirmButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
}

- (void)cancelView
{
//    [self dismissModalViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^(void){}];
}

- (void)submit
{
    [self.textView resignFirstResponder];
    if (self.textView.text.length <= 0) {
        ShowMessage(@"请先输入评论内容");
        return;
    }
    NSMutableDictionary *defaults = [[NSMutableDictionary alloc] init];
    [defaults setObject:self.contentID forKey:@"newsId"];
    [defaults setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] forKey:@"userId"];
    [defaults setObject:self.textView.text forKey:@"context"];
    [[RequestServer instance] doActionAsync:0 withAction:@"com.haoqee.businessSchool.client.action.NewsRemindAction$newRemindReply" withParameters:defaults withDelegate:self];
    [defaults release];
      
}



- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    if (code == 0) {
        ShowMessage(@"回复成功");
        [self dismissViewControllerAnimated:YES completion:^(void){}];
    } else if([code intValue] == 1){
        ShowMessage(message);
    }else{
        ShowMessage(@"回复失败，请重新提交");
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
